
from PySide6.QtWidgets import QWidget,QMessageBox,QVBoxLayout,QTabWidget,QApplication
import sys
import cuslog
import os
from configparser import ConfigParser
from Widget_redis import RedisWidget
from Widget_database import DbWidget
from Widget_svc import SvcWidget
from Widget_logWatcher import LogWidget
from Widget_web import WebWidget


class myMainWindow(QWidget):

    def __init__(self):
        super().__init__()
        self.base_dir = ""
        if getattr(sys, "frozen", False):
            self.base_dir = os.path.dirname(sys.executable)
        else:
            self.base_dir = os.path.dirname(os.path.abspath(__file__))
        os.chdir(self.base_dir)
        self.config = ConfigParser()
        self.cfg_file=os.path.join(self.base_dir, "config.ini")
        # 读取INI文件
        self.config.read(self.cfg_file, encoding="utf-8")

        # 当前路径下的/log 文件夹
        self.log_dir = os.path.join(self.base_dir, "log")
        self.log_filename = "svc.log"
        self.log_path = os.path.join(self.log_dir, self.log_filename)

        self.logger = cuslog.CusLog(self.log_dir, self.log_filename)
        self.ini_ui()

    def closeEvent(self, event):
        reply = QMessageBox.question(
            self,
            "确认",
            "确定要退出吗?",
            QMessageBox.Yes | QMessageBox.No,
            QMessageBox.No,
        )
        
        if reply == QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()

    def save_ini(self):
        with open(
            os.path.join(self.base_dir, "config.ini"), "w", encoding="utf-8"
        ) as f:
            self.config.write(f)

    def ini_ui(self):
        self.setWindowTitle(self.config.get('application','name'))
        self.mainlayout = QVBoxLayout(self)
        self.setLayout(self.mainlayout)
        self.tabup = QTabWidget(self)
        self.tabup.setStyleSheet("QTabBar::tab { min-width: 110px; }")
        self.mainlayout.addWidget(self.tabup)
        self.tab_svc = SvcWidget(self)
        self.tab_db = DbWidget(self)
        self.tab_cache = RedisWidget(self)
        self.tab_web = WebWidget(self)
        
        self.tabup.addTab(self.tab_svc, "服务")
        self.tabup.addTab(self.tab_web, "网站")
        self.tabup.addTab(self.tab_db, "数据库")
        self.tabup.addTab(self.tab_cache, "缓存")

        self.mainlayout.addWidget(self.tabup)

        self.tabdown = QTabWidget(self)
        self.tabdown.setStyleSheet("QTabBar::tab { min-width: 110px; }")
        self.mainlayout.addWidget(self.tabdown)

        self.tab_info = LogWidget(self)
        self.tabdown.addTab(self.tab_info, "运行状态")
        tab4 = QWidget(self)
        self.tabdown.addTab(tab4, "日志文件")


if __name__ == "__main__":
    app = QApplication(sys.argv)
    mainWindow = myMainWindow()
    mainWindow.resize(800, 700)
    mainWindow.show()
    app.exec()
